package com.tusuo.stds.util.webview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.WebView;

import java.util.Map;
import java.util.WeakHashMap;


/**
 * 在应用中加载页面
 *
 * @author J.Huang
 * @version 1.0
 * @since 16/12/13
 */
public class GeneralWebViewClient extends BaseWebViewClient {

    private Activity mActivity;
    private WebView mWebView;

    // 分享

    private boolean mClearHistoryFlag = false;

    GeneralWebViewClient(Activity activity, String loadUrl, WebView webView) {
        super(loadUrl);
        mActivity = activity;
        mWebView = webView;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
//        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
//        mWebView.setVisibility(View.INVISIBLE);

//        if (url.startsWith("http")) {
//            if (mActivity instanceof GeneralActivity_) {
//                ((GeneralActivity_) mActivity).setLastValidUrl(url);
//            }else  if (mActivity instanceof MainActivity_) {
//                ((MainActivity_) mActivity).setLastValidUrl(url);
//            }
//        }
//
//        try {
//            if (mActivity instanceof GeneralActivity_) {
//                if (url.contains("about:blank")) {
//                    ((GeneralActivity_) mActivity).isConnectionFailed(true);
//                } else {
//                    ((GeneralActivity_) mActivity).isConnectionFailed(false);
//                }
//            }else if (mActivity instanceof MainActivity_) {
//                if (url.contains("about:blank")) {
//                    ((MainActivity_) mActivity).isConnectionFailed(true);
//                } else {
//                    ((MainActivity_) mActivity).isConnectionFailed(false);
//                }
//            }
//
//            if (url.contains("PageTitle")) {
//                Uri uri = Uri.parse(url);
//                String pageTitle = uri.getQueryParameter("PageTitle");
//                mToolbar.setToolbarTitle(pageTitle);
//            }
//        } catch (Exception e) {
//        }

        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url == null || TextUtils.isEmpty(url)) {
            return true;
        }

        if (mClearHistoryFlag) {
            view.clearHistory();
            mClearHistoryFlag = false;
        }

        // 下拉刷新
        if (url.equals(mWebView.getUrl())) {
//            CookieHelper.setWebViewRequestUrlCookie(mActivity, url, mCurrentUser.getCurrentCookie());

//            Map<String, String> cookies = new WeakHashMap<>();
//            cookies.put("AppOS", "Android");
            mClearHistoryFlag = true;
//            view.clearView();
//            view.loadUrl(url, cookies);
            return true;
        }

        String lowerCaseUrl = url.toLowerCase().trim();

//        if (lowerCaseUrl.startsWith("tel:")) {
//            return true;
//        }

        // 放在所有特殊操作之后，因为url中可能有js回调函数名(大小写敏感)

        Uri uri = Uri.parse(lowerCaseUrl);

        String path = uri.getPath();

        // 同步Cookie
//        if ("root".equals(pageNav) || "home_index".equalsIgnoreCase(pageName)) {
//            // 获取当前URL的CookieInfo保存到本地数据库
//            String cookie = CookieHelper.getWebViewRequestUrlCookie(mActivity, url);
//
//            if (!cookie.isEmpty()) {
//                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mActivity);
//                SharedPreferences.Editor editor = sharedPref.edit();
//                editor.putString(Constants.KEY_COOKIE, cookie);
//                editor.apply();
//            }
//        }

//        CookieHelper.setWebViewRequestUrlCookie(mActivity, url, mCurrentUser.getCurrentCookie());


        view.clearView();
        Map<String, String> cookies = new WeakHashMap<>();
        cookies.put("AppOS", "Android");

        view.loadUrl(url, cookies);
        // 当返回true时，控制网页在WebView中打开
        // 当返回false时，控制网页在系统自带的或第三方的浏览器中打开
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        // 隐藏输入法
//        Utils.UI.closeInputMethod(mActivity);

        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.clearFocus();

//        mWebView.setVisibility(View.VISIBLE);
//        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

//        if(!mWebView.getSettings().getLoadsImagesAutomatically()) {
//            mWebView.getSettings().setLoadsImagesAutomatically(true);
//        }
    }
}
